package com.jgdt.demo.core;
import com.librariy.util.ViewBinder;
import com.bcia.assetmange.R;
import com.librariy.util.Log;
import com.librariy.util.SystemDisplay;

import android.os.Build;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.appcompat.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
/**
 * activity 父类，统一定标题样式
 * 
 * @author luchyu
 */
public abstract class MultiActivity extends AppCompatActivity{
    private static final String TAG = "MultiActivity";
    protected final SystemDisplay sd = SystemDisplay.getInstance(null);
    protected final ViewBinder mViewBinder = new ViewBinder(this);
    protected ViewGroup mContentView;
    protected int getFragmentId(){
        return mContentView.getId();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        this.mContentView = new FrameLayout(this);
        this.mContentView.setId(android.R.id.widget_frame);
        super.setContentView(mContentView,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
        IntialComponent(savedInstanceState);
    }
    protected void IntialComponent(Bundle savedInstanceState) {
    }
    @Override
    public void setContentView(int layoutResID) {
        mContentView.removeAllViews();
        LayoutInflater.from(this).inflate(layoutResID, mContentView);
    }
    @Override
    public void setContentView(View layoutView) {
        mContentView.removeAllViews();
        mContentView.addView(layoutView,new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
    }
    @Override
    public void setContentView(View layoutView,ViewGroup.LayoutParams params) {
        mContentView.removeAllViews();
        mContentView.addView(layoutView,params);
    }
    public void setBackground(int mColor) {
        mContentView.setBackgroundColor(mColor);
    }
    public void startNewFragment(Fragment fragment, Bundle pare, boolean isBackStack) {
        if(pare!=null){
            fragment.setArguments(pare);
        }
        if(fragment.getArguments()==null){
            fragment.setArguments(new Bundle());
        }
        startNewFragmentInternal(fragment, isBackStack);
    }
    private void startNewFragmentInternal(Fragment fragment, boolean isBackStack) {
        Log.i(TAG,"Displayed startNewFragment=> "+fragment.getClass());
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (getSupportFragmentManager().findFragmentById(mContentView.getId()) != null) {
            ft.setCustomAnimations(R.anim.window_enter, 0, 0, R.anim.window_exit);
        }
        if (isBackStack) {
            ft.replace(getFragmentId(), fragment).addToBackStack(null).commitAllowingStateLoss();
        } else {
            ft.replace(getFragmentId(), fragment).commitAllowingStateLoss();
        }
        performApplyInsetsMonitor();
    }
    @Override
    public void onBackPressed() {
        Fragment fragment = getSupportFragmentManager().findFragmentById(getFragmentId());
        if (fragment instanceof MultiFragment) {
            if (((MultiFragment) fragment).onBackPressed())
                return;
        }
        super.onBackPressed();
    }
    private void performApplyInsetsMonitor() {
        ViewGroup changeView=findViewById(getFragmentId());
        if(changeView==null) return;
        changeView.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() {
            @Override
            public void onChildViewAdded(View parent, View child) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
                    mContentView.requestApplyInsets();
                }
            }
            @Override
            public void onChildViewRemoved(View parent, View child) {}
        });
    }
}